package com.xxx.lib.utils

import android.app.Activity
import android.content.Intent
import com.blankj.utilcode.constant.PermissionConstants
import com.blankj.utilcode.util.PermissionUtils
import com.luck.picture.lib.basic.PictureSelector
import com.luck.picture.lib.config.SelectMimeType
import com.luck.picture.lib.config.SelectModeConfig
import com.luck.picture.lib.entity.LocalMedia
import com.luck.picture.lib.interfaces.OnResultCallbackListener
import com.xxx.lib.GlideEngine
import com.xxx.lib.ImageCompressEngine
import com.xxx.lib.ImageCropEngine
import com.xxx.lib.PathConfig


/**
 * 图片选择器工具类
 */
object PictureSelectorUtils {

    /**
     * 获取图片路径，使用 requestCode 时,通过该方式获取数据
     * @param data 回调的Intent实例
     */
    fun obtainMultipleResult(data: Intent?): ArrayList<LocalMedia>? {
        return PictureSelector.obtainSelectorList(data)
    }

    /**
     * 打开图片选择器，单选
     * @param activity Activity实例
     * @param callBack 回调，callBack、requestCode两者传一
     * @param requestCode 请求码，callBack、requestCode两者传一
     */
    fun openGallerySingle(
        activity: Activity?,
        callBack: OnResultCallbackListener<LocalMedia>? = null,
        requestCode: Int? = null
    ) {
        if (activity == null) return
        PermissionUtils
            .permissionGroup(PermissionConstants.STORAGE)
            .callback(object : PermissionUtils.SimpleCallback {
                override fun onGranted() {
                    PictureSelector
                        .create(activity)
                        .openGallery(SelectMimeType.ofImage())
                        .isDisplayCamera(false)
                        .setCompressEngine(ImageCompressEngine.instance)
                        .setSelectionMode(SelectModeConfig.SINGLE)
                        .isDirectReturnSingle(true)
                        .setImageEngine(GlideEngine.instance)
                        .run {
                            when {
                                callBack != null -> forResult(callBack)
                                requestCode != null -> forResult(requestCode)
                            }
                        }
                }

                override fun onDenied() {}
            })
            .request()
    }

    /**
     * 打开图片选择器，单选，裁剪
     * @param activity Activity实例
     * @param aspectRatioX 裁剪比例
     * @param aspectRatioY 裁剪比例
     * @param callBack 回调，callBack、requestCode两者传一
     * @param requestCode 请求码，callBack、requestCode两者传一
     */
    fun openGallerySingleCrop(
        activity: Activity?,
        aspectRatioX: Float,
        aspectRatioY: Float,
        callBack: OnResultCallbackListener<LocalMedia>? = null,
        requestCode: Int? = null
    ) {
        if (activity == null) return
        PermissionUtils
            .permissionGroup(PermissionConstants.STORAGE)
            .callback(object : PermissionUtils.SimpleCallback {
                override fun onGranted() {
                    PictureSelector
                        .create(activity)
                        .openGallery(SelectMimeType.ofImage())
                        .isDisplayCamera(false)
                        .setCompressEngine(ImageCompressEngine.instance)
                        .setSelectionMode(SelectModeConfig.SINGLE)
                        .isDirectReturnSingle(true)
                        .setCropEngine(ImageCropEngine(aspectRatioX, aspectRatioY))
                        .setImageEngine(GlideEngine.instance)
                        .run {
                            when {
                                callBack != null -> forResult(callBack)
                                requestCode != null -> forResult(requestCode)
                            }
                        }
                }

                override fun onDenied() {}
            })
            .request()
    }

    /**
     * 打开图片选择器，多选
     * @param activity Activity实例
     * @param max 最大选择数
     * @param callBack 回调，callBack、requestCode两者传一
     * @param requestCode 请求码，callBack、requestCode两者传一
     */
    fun openGalleryMultiple(
        activity: Activity?,
        max: Int,
        callBack: OnResultCallbackListener<LocalMedia>? = null,
        requestCode: Int? = null
    ) {
        if (activity == null) return
        PermissionUtils
            .permissionGroup(PermissionConstants.STORAGE)
            .callback(object : PermissionUtils.SimpleCallback {
                override fun onGranted() {
                    PictureSelector
                        .create(activity)
                        .openGallery(SelectMimeType.ofImage())
                        .isDisplayCamera(false)
                        .setCompressEngine(ImageCompressEngine.instance)
                        .setMaxSelectNum(max)
                        .setImageEngine(GlideEngine.instance)
                        .run {
                            when {
                                callBack != null -> forResult(callBack)
                                requestCode != null -> forResult(requestCode)
                            }
                        }
                }

                override fun onDenied() {}
            })
            .request()
    }

    /**
     * 打开相机
     * @param activity Activity实例
     * @param callBack 回调，callBack、requestCode两者传一
     * @param requestCode 请求码，callBack、requestCode两者传一
     */
    fun openCamera(
        activity: Activity?,
        callBack: OnResultCallbackListener<LocalMedia>? = null,
        requestCode: Int? = null
    ) {
        if (activity == null) return
        PermissionUtils
            .permissionGroup(PermissionConstants.STORAGE, PermissionConstants.CAMERA)
            .callback(object : PermissionUtils.SimpleCallback {
                override fun onGranted() {
                    PictureSelector.create(activity)
                        .openCamera(SelectMimeType.ofImage())
                        .setOutputCameraDir(PathConfig.DIR_CACHE_PICTURESELECTOR)
                        .setCompressEngine(ImageCompressEngine.instance)
                        .run {
                            when {
                                callBack != null -> forResult(callBack)
                                requestCode != null -> forResultActivity(requestCode)
                            }
                        }
                }

                override fun onDenied() {}
            })
            .request()
    }


    /**
     * 打开相机，裁剪
     * @param activity Activity实例
     * @param aspectRatioX 裁剪比例
     * @param aspectRatioY 裁剪比例
     * @param callBack 回调，callBack、requestCode两者传一
     * @param requestCode 请求码，callBack、requestCode两者传一
     */
    fun openCameraCrop(
        activity: Activity?,
        aspectRatioX: Float,
        aspectRatioY: Float,
        callBack: OnResultCallbackListener<LocalMedia>? = null,
        requestCode: Int? = null
    ) {
        if (activity == null) return
        PermissionUtils
            .permissionGroup(PermissionConstants.STORAGE, PermissionConstants.CAMERA)
            .callback(object : PermissionUtils.SimpleCallback {
                override fun onGranted() {
                    PictureSelector.create(activity)
                        .openCamera(SelectMimeType.ofImage())
                        .setOutputCameraDir(PathConfig.DIR_CACHE_PICTURESELECTOR)
                        .setCompressEngine(ImageCompressEngine.instance)
                        .setCropEngine(ImageCropEngine(aspectRatioX, aspectRatioY))
                        .run {
                            when {
                                callBack != null -> forResult(callBack)
                                requestCode != null -> forResultActivity(requestCode)
                            }
                        }
                }

                override fun onDenied() {}
            })
            .request()
    }


}